package handle

import (
	"gitee.com/crack007/goose"
	"gitee.com/crack007/goose/core/fork"
	"gitee.com/crack007/goose/core/server"
	"gitee.com/crack007/goose/core/web"
	"gitee.com/crack007/udp-penetrate/src/api/config"
	"gitee.com/crack007/udp-penetrate/src/api/router"
	"gitee.com/crack007/udp-penetrate/src/common/protocol"
	"gitee.com/crack007/udp-penetrate/src/udpserver/handle"
)

type serverApiHandle struct {
	web.DefaultHttpServerHandle
}

func NewServerApiHandle() *serverApiHandle {
	return &serverApiHandle{}
}

func (h *serverApiHandle) OnInitConfig() {
	config.Init(h.GetHttpServer().GetConfigPath())
}
func (h *serverApiHandle) OnInitRouter() {
	router.ServerApi(h.GetHttpServer())
}

func (h *serverApiHandle) OnInitService() {
	protocol.RegisterProtocol(protocol.NewHandshakeReqProtocol())
	protocol.RegisterProtocol(protocol.NewClientHandshakeRespProtocol())
	protocol.RegisterProtocol(protocol.NewConnectReqProtocol())

}
func (h *serverApiHandle) OnStartSlaveServer() {
	sessionBuilder := server.NewSessionBuilder(handle.NewUdpServerSessionHandle())
	serverBuilder := fork.NewServerBuilderFromWeb(h.GetHttpServer(), handle.NewUdpServerHandle(), sessionBuilder)
	serverBuilder.SetProtocol("udp")
	serverBuilder.SetPort(config.AppConfig.ServerPort)
	serverBuilder.SetServerName("udpServer")
	goose.ExecuteSlave(serverBuilder)
}
